package com.open.party.cloud.view.home.dangYuanJiaoYu

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.util.Log
import cn.android.x.model.data.ResultInfo
import cn.android.x.plugins.webView.ScrollWebView
import cn.android.x.plugins.webView.WebViewForFilePreviewActivity
import com.open.party.cloud.model.StudyRecordBean
import com.open.party.cloud.model.StudyRecordVo
import com.open.party.cloud.viewModel.StudyViewModel
import com.sinothk.android.utils.XUtils
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class YunKeTangStudyFileActivity : WebViewForFilePreviewActivity() {

    companion object {

        @JvmStatic
        fun start(activity: Activity?, bundle: Bundle) {
            val intent = Intent(activity, YunKeTangStudyFileActivity::class.java)
            intent.putExtras(bundle)
            activity?.startActivity(intent)
        }

        fun start(activity: Activity?, titleTxt: String, url: String) {
            val bundle = Bundle()
            bundle.putString("titleTxt", titleTxt)
            bundle.putString("url", url)
            val intent = Intent(activity, YunKeTangStudyFileActivity::class.java)
            intent.putExtras(bundle)
            activity?.startActivity(intent)
        }
    }

    private var isStart = false
    private var isFinish = false
    private var cmId = ""
    private var learnId = ""

    var viewModel: StudyViewModel? = null
    var startTime = 0L


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        isStart = intent.getBooleanExtra("isStart", false)
        isFinish = intent.getBooleanExtra("isFinish", false)

        cmId = intent.getStringExtra("cmId") as String
        learnId = intent.getStringExtra("learnId") as String

        viewModel = StudyViewModel()
        startTime = XUtils.date().time

        rootWebView?.setOnScrollEventListener(object : ScrollWebView.OnScrollEventListener {
            override fun onPageTop(l: Int, t: Int, oldl: Int, oldt: Int) {
            }

            override fun onScrollChanged(l: Int, t: Int, oldl: Int, oldt: Int) {
            }

            @SuppressLint("LongLogTag")
            override fun onPageBottom(l: Int, t: Int, oldl: Int, oldt: Int) {
//                Log.e("setOnScrollChangeListener", "isBottom => 学习完了")
                if (!isFinish) {
                    val times = XUtils.date().time - startTime
                    val vo = getRecordBean(true, times / 1000)
                    viewModel!!.saveOrUpdateStudyRecord(vo, "isFinish")
                }
            }
        })

        if (!isStart && !isFinish) {
            val vo: StudyRecordVo = getRecordBean(false, 0)
            viewModel!!.saveOrUpdateStudyRecord(vo, "isStart")
        }
    }

    private fun getRecordBean(isFinish: Boolean, times: Long): StudyRecordVo {
        val vo = StudyRecordVo()
        vo.state = isFinish
        vo.learnId = XUtils.string().getNotNullValue(learnId)
        vo.cmId = XUtils.string().getNotNullValue(cmId)
        vo.learningDuration = times
        return vo
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public fun eventBusCallback(result: ResultInfo<StudyRecordBean>) {
        if ("saveOrUpdateStudyRecord" != result.eventType) {
            return
        }

        when (result.code) {
            ResultInfo.SUCCESS -> {
                if (result.data != null) {

                    if ("isStart"== result.msg) {
                        isStart = true
                    }
                    if ("isFinish"== result.msg) {
                        isFinish = true
                    }

                    XUtils.toast().show("更新记录成功")
                }
            }
            ResultInfo.TOKEN_OVERDUE -> {
                XUtils.toast().show("Token过期，请重新登录")
            }
            else -> {
                XUtils.toast().show(result.msg)
            }
        }
    }

    override fun onResume() {
        super.onResume()
        EventBus.getDefault().register(this)
    }

    override fun onPause() {
        super.onPause()
        EventBus.getDefault().unregister(this)
    }
}
